using com.haopintui.entity;
using com.haopintui.ui;
using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace com.haopintui.util
{
	public class CmsUtil
	{
		public static void view_cms(CmsForm cmsForm)
		{
			CmsUI cmsUI = cmsForm.cmsUI;
			if (!cmsUI.comboBoxCmPPos.Enabled)
			{
				return;
			}
			if (!cmsUI.comboBoxCmPUnit.Enabled)
			{
				return;
			}
			cmsUI.comboBoxCmPPos.Items.Clear();
			cmsUI.comboBoxCmPUnit.Items.Clear();
			foreach (AdZonesItem adZonesItem in cmsForm.adzoneBean.webAdzones_List)
			{
				SelectedItem selectedItem = new SelectedItem();
				selectedItem.setId(adZonesItem.id);
				selectedItem.setLabel(adZonesItem.name);
				selectedItem.setValue(adZonesItem.sub_list);
				cmsUI.comboBoxCmPUnit.Items.Add(selectedItem);
			}
			if (cmsUI.comboBoxCmPUnit.Items.Count > 0)
			{
				cmsUI.comboBoxCmPUnit.SelectedIndex = 0;
			}
		}
		public static void view_cms_call(CmsForm cmsForm)
		{
			CmsUI cmsUI = cmsForm.cmsUI;
			if (cmsUI.comboBoxCmPUnit.InvokeRequired)
			{
				view_cms_call method = new view_cms_call(CmsUtil.view_cms_call);
				cmsForm.Invoke(method, new object[]
				{
					cmsForm
				});
				return;
			}
			CmsUtil.view_cms(cmsForm);
		}
		public static void comboBoxBrdgPUnit_SelectedIndexChanged(CmsForm cmsForm)
		{
			try
			{
				CmsUI cmsUI = cmsForm.cmsUI;
				cmsUI.comboBoxCmPPos.Items.Clear();
				SelectedItem selectedItem = (SelectedItem)cmsUI.comboBoxCmPUnit.SelectedItem;
				ArrayList arrayList = (ArrayList)selectedItem.getValue();
				foreach (AdZonesSubItem adZonesSubItem in arrayList)
				{
					SelectedItem selectedItem2 = new SelectedItem();
					selectedItem2.setLabel(adZonesSubItem.name);
					selectedItem2.setId(adZonesSubItem.id);
					cmsUI.comboBoxCmPPos.Items.Add(selectedItem2);
				}
				if (cmsUI.comboBoxCmPPos.Items.Count > 0)
				{
					cmsUI.comboBoxCmPPos.SelectedIndex = 0;
				}
			}
			catch (Exception ex)
			{
				LogUtil.log_str(cmsForm, "[comboBoxBrdgPUnit_SelectedIndexChanged]出错：" + ex.ToString());
			}
		}
		internal static void update_cms_list(object obj)
		{
			CmsForm cmsForm = (CmsForm)obj;
			ArrayList arrayList = UserUtil.query_cms_list(cmsForm);
			CmsUI cmsUI = cmsForm.cmsUI;
			cmsUI.comboBoxCmsList.Items.Clear();
			foreach (UserCms userCms in arrayList)
			{
				SelectedItem selectedItem = new SelectedItem();
				selectedItem.setId(userCms.app_id);
				selectedItem.setLabel(userCms.title);
				cmsUI.comboBoxCmsList.Items.Add(selectedItem);
			}
			if (cmsUI.comboBoxCmsList.Items.Count > 0)
			{
				cmsUI.comboBoxCmsList.SelectedIndex = 0;
			}
		}
		internal static void update_cms_list_call(object obj)
		{
			CmsForm cmsForm = (CmsForm)obj;
			CmsUI cmsUI = cmsForm.cmsUI;
			while (!cmsForm.IsHandleCreated)
			{
			}
			if (cmsUI.comboBoxCmsList.InvokeRequired)
			{
				update_cms_list_call method = new update_cms_list_call(CmsUtil.update_cms_list_call);
				cmsForm.Invoke(method, new object[]
				{
					cmsForm
				});
				return;
			}
			CmsUtil.update_cms_list(cmsForm);
			IEVersion.BrowserEmulationSet();
		}
		public static void cms_apply_taoke_url(CmsForm cmsForm, int apply_type)
		{
			if (apply_type != 1)
			{
				CmsUI cmsUI = cmsForm.cmsUI;
				cmsUI.button_cms_click_apply.Text = "开始自动批量转化";
				cmsUI.button_cms_click_apply.UseVisualStyleBackColor = true;
				cmsUI.button_cms_click_apply.BackColor = Color.Green;
				cmsUI.button_cms_click_apply.ForeColor = Color.White;
				cmsForm.appBean.apply_taoke_url_status = false;
				cmsUI.comboBoxCmPUnit.Enabled = true;
				cmsUI.comboBoxCmPPos.Enabled = true;
				cmsUI.comboBoxCmsList.Enabled = true;
				return;
			}
			CmsUI cmsUI2 = cmsForm.cmsUI;
			SelectedItem selectedItem = (SelectedItem)cmsUI2.comboBoxCmPUnit.SelectedItem;
			if (selectedItem == null)
			{
				selectedItem = new SelectedItem();
			}
			SelectedItem selectedItem2 = (SelectedItem)cmsUI2.comboBoxCmPPos.SelectedItem;
			if (selectedItem2 == null)
			{
				selectedItem2 = new SelectedItem();
			}
			SelectedItem selectedItem3 = (SelectedItem)cmsUI2.comboBoxCmsList.SelectedItem;
			if (selectedItem3 == null)
			{
				selectedItem3 = new SelectedItem();
			}
			LogUtil.log_str(cmsForm, selectedItem3.getLabel() ?? "");
			if (selectedItem.getId() == string.Empty)
			{
				MessageBox.Show("推广单元不能为空！");
				return;
			}
			if (selectedItem2.getId() == string.Empty)
			{
				MessageBox.Show("推广位不能为空！");
				return;
			}
			if (selectedItem3.getId() == string.Empty)
			{
				MessageBox.Show("CMS不能为空！");
				return;
			}
			cmsUI2.button_cms_click_apply.Text = "停止批量转化";
			cmsUI2.button_cms_click_apply.BackColor = Color.Gray;
			cmsUI2.button_cms_click_apply.ForeColor = Color.Black;
			cmsForm.appBean.apply_taoke_url_status = true;
			cmsForm.appBean.applyreason = cmsUI2.textBoxAppCmsReson.Text;
			cmsUI2.comboBoxCmPUnit.Enabled = false;
			cmsUI2.comboBoxCmPPos.Enabled = false;
			cmsUI2.comboBoxCmsList.Enabled = false;
			cmsForm.appBean.cms_siteid = (string)selectedItem.getId();
			cmsForm.appBean.cms_adzoneid = (string)selectedItem2.getId();
			cmsForm.appBean.cms_app_id = (string)selectedItem3.getId();
			new Thread(new ParameterizedThreadStart(CmsUtil.apply_cms_url))
			{
				IsBackground = true
			}.Start(cmsForm);
			new Thread(new ParameterizedThreadStart(CmsUtil.tongji_user_cms))
			{
				IsBackground = true
			}.Start(cmsForm);
		}
		internal static void apply_cms_url(object obj)
		{
			CmsForm cmsForm = (CmsForm)obj;
			try
			{
				LogUtil.log_cms_call(cmsForm, "开始批量转化");
				int num = 0;
				TaokeItem taokeItem = null;
				int num2 = 0;
				while (cmsForm.appBean.apply_taoke_url_status)
				{
					try
					{
						if (num == 0)
						{
							if (taokeItem == null)
							{
								num2 = 0;
							}
							CmsUtil.apply_taoke_url(cmsForm, ref num, ref taokeItem, ref num2);
							if (num == 1)
							{
								LogUtil.log_cms_call(cmsForm, "转化失败。检查是否阿里妈妈登录过期");
							}
						}
						if (num == 1)
						{
							if (!AlimamaUtil.check_login(cmsForm.appBean.taoke_cookie) && Process.GetProcessesByName("AlimamaLogin").Length <= 0)
							{
								LogUtil.log_cms_call(cmsForm, "阿里妈妈登录过期。正在开始重新登录");
								if (cmsForm.configUI.checkBoxAutoLogin.Checked)
								{
									AlimamaLogin.login(cmsForm);
								}
								else
								{
									LogUtil.log_cms_call(cmsForm, "没有开启阿里妈妈自动登录，无法完成登录");
								}
							}
							else
							{
								if (AlimamaUtil.check_login(cmsForm.appBean.taoke_cookie))
								{
									LogUtil.log_cms_call(cmsForm, "阿里妈妈登录正常");
									num = 0;
								}
								else
								{
									if (Process.GetProcessesByName("AlimamaLogin").Length > 0)
									{
										LogUtil.log_cms_call(cmsForm, "登录窗口正在运行中");
										Thread.Sleep(3000);
									}
								}
							}
						}
						else
						{
							if (num == 2)
							{
								LogUtil.log_cms_call(cmsForm, "转化过快。阿里妈妈限制了频率，将暂停30秒继续");
								Thread.Sleep(30000);
								num = 0;
							}
						}
						Thread.Sleep(cmsForm.appBean.alimama_request_url_time * 1000);
					}
					catch (Exception ex)
					{
						LogUtil.log_call(cmsForm, ex.ToString() ?? "");
					}
				}
				LogUtil.log_cms_call(cmsForm, "停止批量转化");
			}
			catch (Exception ex2)
			{
				LogUtil.log_call(cmsForm, "[checkAutoLogin]出错！" + ex2.ToString());
			}
		}
		public static void apply_taoke_url(CmsForm cmsForm, ref int status, ref TaokeItem taoke_item_init, ref int count_xunhuan)
		{
			if (taoke_item_init != null)
			{
				count_xunhuan++;
				TaokeItem taokeItem = taoke_item_init;
				LogUtil.log_cms_call(cmsForm, string.Concat(new string[]
				{
					"获取新的转化商品【",
					taokeItem.title,
					"】【id：",
					taokeItem.num_iid,
					"】"
				}));
				CmsUtil.apply_taoke_url_item(cmsForm, taokeItem, ref status);
				if (status == 0)
				{
					taoke_item_init = null;
					return;
				}
			}
			else
			{
				ArrayList arrayList = UserUtil.query_taoke_item_list(cmsForm);
				if (arrayList != null && arrayList.Count > 0)
				{
					foreach (TaokeItem taokeItem2 in arrayList)
					{
						taoke_item_init = taokeItem2;
						if (taokeItem2.taoke_status == 0)
						{
							LogUtil.log_cms_call(cmsForm, string.Concat(new string[]
							{
								"商品【",
								taokeItem2.title,
								"】【id：",
								taokeItem2.num_iid,
								"】【",
								taokeItem2.taoke_status_err,
								"】"
							}));
							taoke_item_init = null;
						}
						else
						{
							LogUtil.log_cms_call(cmsForm, string.Concat(new string[]
							{
								"获取新的转化商品【",
								taokeItem2.title,
								"】【id：",
								taokeItem2.num_iid,
								"】"
							}));
							CmsUtil.apply_taoke_url_item(cmsForm, taokeItem2, ref status);
							if (status == 0)
							{
								taoke_item_init = null;
							}
						}
					}
				}
			}
		}
		public static void apply_taoke_url_item(CmsForm cmsForm, TaokeItem taokeItem, ref int status)
		{
			if (taokeItem.num_iid != string.Empty)
			{
				string text = "";
				ArrayList arrayList = AlimamaUtil.query_campaign_list(cmsForm.appBean.taoke_cookie, taokeItem.num_iid, out text);
				CampaignItem1 campaignItem = null;
				CampaignItem1 campaignItem2 = null;
				if (arrayList != null && arrayList.Count > 0)
				{
					IEnumerator enumerator = arrayList.GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							CampaignItem1 campaignItem3 = (CampaignItem1)enumerator.Current;
							if (campaignItem3.commissionRate > taokeItem.tkRate && (campaignItem == null || campaignItem.commissionRate < campaignItem3.commissionRate))
							{
								campaignItem = campaignItem3;
							}
							if (campaignItem3.commissionRate > taokeItem.tkRate && campaignItem3.manualAudit == "0" && (campaignItem2 == null || campaignItem2.commissionRate < campaignItem3.commissionRate))
							{
								campaignItem2 = campaignItem3;
							}
						}
						goto IL_FF;
					}
					finally
					{
						IDisposable disposable = enumerator as IDisposable;
						if (disposable != null)
						{
							disposable.Dispose();
						}
					}
				}
				if (text.Contains("\"wait\""))
				{
					status = 2;
					LogUtil.log_cms_call(cmsForm, "[query_campaign:]" + text);
					return;
				}
				IL_FF:
				text = "";
				if (campaignItem2 != null)
				{
					if (campaignItem2.Exist == "true")
					{
						LogUtil.log_cms_call(cmsForm, "定向计划【" + campaignItem2.CampaignName + "】已经申请过。本次跳过重复申请");
					}
					else
					{
						LogUtil.log_cms_call(cmsForm, "开始申请定向计划【" + campaignItem2.CampaignName + "】");
						AlimamaUtil.apply_campaign(campaignItem2.CampaignID, campaignItem2.ShopKeeperID, Constants.applyreason_pre, cmsForm.appBean.applyreason, cmsForm.appBean.taoke_cookie, out text);
						if (text.Contains("\"wait\""))
						{
							status = 2;
							LogUtil.log_cms_call(cmsForm, "[apply_campaign_auto:]" + text);
							return;
						}
					}
				}
				else
				{
					if (campaignItem != null)
					{
						if (campaignItem.Exist == "true")
						{
							LogUtil.log_cms_call(cmsForm, "定向计划【" + campaignItem.CampaignName + "】已经申请过。本次跳过重复申请");
						}
						else
						{
							LogUtil.log_cms_call(cmsForm, "开始申请定向计划【" + campaignItem.CampaignName + "】");
							AlimamaUtil.apply_campaign(campaignItem.CampaignID, campaignItem.ShopKeeperID, Constants.applyreason_pre, cmsForm.appBean.applyreason, cmsForm.appBean.taoke_cookie, out text);
							if (text.Contains("\"wait\""))
							{
								status = 2;
								LogUtil.log_cms_call(cmsForm, "[apply_campaign:]" + text);
								return;
							}
						}
					}
				}
				if (text != null && text != "")
				{
					LogUtil.log_cms_call(cmsForm, text);
				}
				GoodsItem2 goodsItem = AlimamaUtil.query_taoke_item_event(cmsForm.appBean.taoke_cookie, taokeItem.num_iid, out text);
				int num = 0;
				if (goodsItem != null && taokeItem.tkRate < goodsItem.eventRate && (campaignItem2 == null || campaignItem2.commissionRate < goodsItem.eventRate))
				{
					num = 1;
				}
				else
				{
					if (text.Contains("\"wait\""))
					{
						status = 2;
						LogUtil.log_cms_call(cmsForm, "[query_campaign:]" + text);
						return;
					}
				}
				if (num == 1)
				{
					LogUtil.log_cms_call(cmsForm, "通过鹊桥计划转换链接：佣金比例【" + goodsItem.eventRate + "%】");
				}
				else
				{
					if (campaignItem2 != null)
					{
						LogUtil.log_cms_call(cmsForm, "通过定向计划转换链接：佣金比例【" + campaignItem2.commissionRate + "%】");
					}
					else
					{
						if (campaignItem != null)
						{
							LogUtil.log_cms_call(cmsForm, string.Concat(new object[]
							{
								"通过通用计划转换链接：佣金比例【",
								taokeItem.tkRate,
								"%】 定向计划【",
								campaignItem.CampaignName,
								"】审核通过后，会自动走高佣金【",
								campaignItem.commissionRate,
								"%】"
							}));
						}
						else
						{
							LogUtil.log_cms_call(cmsForm, "通过通用计划转换链接：佣金比例【" + taokeItem.tkRate + "%】");
						}
					}
				}
				AlimamaClick alimamaClick = AlimamaUtil.query_taoke_click(cmsForm.appBean.taoke_cookie, taokeItem.num_iid, cmsForm.appBean.cms_adzoneid, cmsForm.appBean.cms_siteid, num, out text);
				if (alimamaClick != null)
				{
					string text2 = CmsUtil.apply_cms_taokouling_url(cmsForm, taokeItem.num_iid, taokeItem.activity_id, string.Concat(new string[]
					{
						"mm_",
						cmsForm.appBean.member_id,
						"_",
						cmsForm.appBean.cms_siteid,
						"_",
						cmsForm.appBean.cms_adzoneid
					}));
					if (!string.IsNullOrEmpty(text2))
					{
						LogUtil.log_cms_call(cmsForm, "获取到淘口令：[" + text2 + "]");
						alimamaClick.coupon_link_tao_token = text2;
						Constants.set_ie_boolean = true;
					}
					else
					{
						if (!Constants.set_ie_boolean)
						{
							LogUtil.log_cms_call(cmsForm, "***********提醒：[可能是配置淘口令环境未成功，请右键一以管理员身份运行助手目录下的“配置淘口令环境”程序]*********当前IE版本：[" + cmsForm.cmsUI.webBrowserQuanAlimama.Version.ToString() + "]");
						}
					}
					UserUtil.submit_taoke_item(cmsForm, alimamaClick);
					return;
				}
				if (text.Contains("\"wait\""))
				{
					status = 2;
					LogUtil.log_cms_call(cmsForm, "[click:]" + text);
					return;
				}
				if (text.Contains("callLocation") || text.Contains("gen url item not found on call"))
				{
					LogUtil.log_cms_call(cmsForm, "[该商品已经下架或者退出了淘客推广。后期系统将会将此商家列为黑名单商家:]");
					return;
				}
				status = 1;
				if (text != null && text != "")
				{
					LogUtil.log_cms_call(cmsForm, text);
				}
			}
		}
		internal static void tongji_user_cms(object obj)
		{
			CmsForm cmsForm = (CmsForm)obj;
			try
			{
				LogUtil.log_call(cmsForm, "统计开始");
				while (cmsForm.appBean.apply_taoke_url_status)
				{
					string text;
					UserCmsTongji userCmsTongji = UserUtil.query_user_cms_tongji(cmsForm, out text);
					if (userCmsTongji != null)
					{
						CmsUtil.update_cms_tongji_call(cmsForm, userCmsTongji);
					}
					Thread.Sleep(1000);
				}
				LogUtil.log_call(cmsForm, "统计停止");
			}
			catch (Exception ex)
			{
				LogUtil.log_call(cmsForm, "[checkAutoLogin]出错！" + ex.ToString());
			}
		}
		internal static void update_cms_tongji_call(CmsForm cmsForm, UserCmsTongji userCmsTongji)
		{
			CmsUI cmsUI = cmsForm.cmsUI;
			if (cmsUI.label39.InvokeRequired)
			{
				update_cms_tongji_call method = new update_cms_tongji_call(CmsUtil.update_cms_tongji_call);
				cmsForm.Invoke(method, new object[]
				{
					cmsForm,
					userCmsTongji
				});
				return;
			}
			cmsForm.cmsUI.label39.Text = "已转化：" + userCmsTongji.have;
			cmsForm.cmsUI.label40.Text = "未转化：" + userCmsTongji.all;
		}
		internal static string apply_cms_taokouling_url(CmsForm cmsForm, string num_iid, string activity_id, string pid)
		{
			try
			{
				CmsUtil.update_user_agent_Handler(cmsForm);
				string urlString = string.Concat(new string[]
				{
					"https://uland.taobao.com/coupon/edetail?activityId=",
					activity_id,
					"&pid=",
					pid,
					"&itemId=",
					num_iid,
					"&src=qhkj_dtkp&dx="
				});
				cmsForm.cmsUI.webBrowserQuanAlimama.Navigate(urlString);
				cmsForm.appBean.taokouling = null;
				int num = 0;
				while (num < 50 && string.IsNullOrEmpty(cmsForm.appBean.taokouling))
				{
					Thread.Sleep(100);
					num++;
				}
				if (!string.IsNullOrEmpty(cmsForm.appBean.taokouling))
				{
					string taokouling = cmsForm.appBean.taokouling;
					cmsForm.cmsUI.webBrowserQuanAlimama.Navigate("about:blank");
					cmsForm.appBean.taokouling = null;
					return taokouling;
				}
				cmsForm.cmsUI.webBrowserQuanAlimama.Navigate("about:blank");
			}
			catch (Exception ex)
			{
				cmsForm.cmsUI.webBrowserQuanAlimama.Navigate("about:blank");
				LogUtil.log_call(cmsForm, "[获取淘口令]出错！" + ex.ToString());
			}
			return null;
		}
		internal static void update_user_agent_Handler(object obj)
		{
			CmsForm cmsForm = (CmsForm)obj;
			CmsUI cmsUI = cmsForm.cmsUI;
			if (cmsUI.webBrowserQuanAlimama.InvokeRequired)
			{
				update_user_agent_Handler method = new update_user_agent_Handler(CmsUtil.update_user_agent_Handler);
				cmsForm.Invoke(method, new object[]
				{
					cmsForm
				});
				return;
			}
			UserAgentHelper.AppendUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
		}
	}
}
